Skip Navigation

[Resuelto] Views Conditional wpv-post-author problem

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem: I have a conditional that tests the current User against the author of a post. The conditional works in the context of a post, but I would like to use the same conditional in another page where the post ID is set by a URL parameter.

Solution: You could use a custom shortcode like this instead:

[wpv-conditional if="('[wpv-current-user info='user_login']' eq '[author_login_from_post_id_param]')"]
Content 2
[/wpv-conditional]

Add this code to your child theme's functions.php file:

function author_login_from_post_id_param_func($atts) {
  $postid = isset($_GET['postid']) ? $_GET['postid'] : 0;
  $thepost = $postid ? get_post($postid) : 0;
  $author = $thepost ? $thepost->post_author : 0;
  $login = $author ? get_the_author_meta('user_login', $author) : '';
 
  return $login;
}
add_shortcode("author_login_from_post_id_param", "author_login_from_post_id_param_func");

Then go to Toolset > Settings > Frontend Content and register author_login_from_post_id_param in the third-party shortcodes section.

This support ticket is created hace 6 años, 6 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Etiquetado: 

Este tema contiene 2 respuestas, tiene 2 mensajes.

Última actualización por Tim Elliott hace 6 años, 6 meses.

Asistido por: Christian Cox.

Autor
Mensajes
#905666

Hi Support team,

I have a content template with some conditional filters.

[wpv-conditional if="('[wpv-current-user info='user_login']' ne '[wpv-post-author format="meta" meta="user_login"]')"]
Content 1
[/wpv-conditional]

[wpv-conditional if="('[wpv-current-user info='user_login']' eq '[wpv-post-author format="meta" meta="user_login"]')"]
  Content 2
[/wpv-conditional]

This works fine when the content template embedded in a relevant custom post.

What I am now trying to do is have the same conditionals for the same custom post, but embedded in a different page. I'm using URL parameters to get the info into the content template, e.g.:
enlace oculto show="single"]&postid=[wpv-post-id]&postname=[wpv-post-title]

On the "Claim Tool" Page I want to use the same conditionals. I've tested and the following shortcodes output the correct (matching) wordpress user id.
Page Owner (of custom post):
[wpv-post-author format="meta" meta="user_login" id="[wpv-search-term param='postid']"]

Current User
[wpv-current-user info='user_login']

But when I try to plug them into the conditional it doesn't work:

[wpv-conditional if="('[wpv-current-user info='user_login']' eq '[wpv-post-author format="meta" meta="user_login" id="[wpv-search-term param='postid']"]')"]
Content 2
[/wpv-conditional]

Do I have too many shortcodes-within-shortcodes? Are the single and double quote conflicting with each other?

Thanks for your help.

#905795

Hi, yes I believe you're bumping into the upper limit of nested shortcodes here. You could use a custom shortcode like this instead:

[wpv-conditional if="('[wpv-current-user info='user_login']' eq '[author_login_from_post_id_param]')"]
Content 2
[/wpv-conditional]

Add this code to your child theme's functions.php file:

function author_login_from_post_id_param_func($atts) {
  $postid = isset($_GET['postid']) ? $_GET['postid'] : 0;
  $thepost = $postid ? get_post($postid) : 0;
  $author = $thepost ? $thepost->post_author : 0;
  $login = $author ? get_the_author_meta('user_login', $author) : '';

  return $login;
}
add_shortcode("author_login_from_post_id_param", "author_login_from_post_id_param_func");

Then go to Toolset > Settings > Frontend Content and register author_login_from_post_id_param in the third-party shortcodes section.

#905814

Thanks Christian. That's done the trick.

Thanks for your help.