In WordPress verwende ich eine Funktion in functions.php, um bestimmte Beiträge (nach Kategorie) nur dann nicht anzuzeigen, wenn ein Benutzer nicht angemeldet ist:
function my_filter( $content ) {
$categories = array(
'news',
'opinions',
'sports',
'other',
);
if ( in_category( $categories ) ) {
if ( is_logged_in() ) {
return $content;
} else {
$content = '<p>Sorry, this post is only available to members</p> <a href="gateblogs.com/login"> Login </a>';
return $content;
}
} else {
return $content;
}
}
add_filter( 'the_content', 'my_filter' );
Ich verwende ein Plugin (für meine Frage nicht wichtig), kann aber nach einer erfolgreichen Anmeldung mit https://gateblogs.com/login?redirect_to=https%3A%2F%2Fgateblogs.com%2Ftechnology%2Flinux-tutorials%2Fsending-mail-from-server
zu einer Seite umleiten. Wie kann ich die URL eines Posts abrufen und in den Anmeldelink übernehmen?.
Hinweis: Die Funktion stammt ursprünglich aus dieser Frage .
Ich habe herausgefunden, wie es mit der Funktion get_the_permalink()
geht:
/* Protect Member Only Posts*/
function post_filter( $content ) {
$categories = array(
'coding',
'python',
'linux-tutorials',
'Swift',
'premium',
);
if ( in_category( $categories ) ) {
if ( is_user_logged_in() ) {
return $content;
} else {
$link = get_the_permalink();
$link = str_replace(':', '%3A', $link);
$link = str_replace('/', '%2F', $link);
$content = "<p>Sorry, this post is only available to members. <a href=\"gateblogs.com/login?redirect_to=$link\">Sign in/Register</a></p>";
return $content;
}
} else {
return $content;
}
}
add_filter( 'the_content', 'post_filter' );
Bitte beachte, dass der str_replace
ist, weil ich den Link ändern muss, damit das Plugin funktioniert.
Sie können eine Bedingung erstellen und die URL des Posts zurückgeben, indem Sie eine get_the_permalink()
in Verbindung mit is_single()
verwenden:
add_filter( 'the_content', 'my_filter' );
function my_filter( $content ) {
// Check if we're inside the main loop in a single post page.
if ( is_single() && in_the_loop() && is_main_query() ) {
return $content . get_the_permalink();
}
return $content;
}