Below the code I have on my page tot display the permalink of q specific post in Wordpress. It works, but I have the feeling it can be easier. Can somebody explain how?
$post_id = 26; // post id
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
$content = $queried_post->post_content;
$perma = get_permalink($post_id);
if ( has_post_thumbnail() ) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
}
echo '<a href="' . $perma . '" title="' . $title . '">';
echo $title;
echo '</a>';
echo '<img width="100%" src="' . $image_src[0] . '">';
echo $content;
Well your code seems ok. However there are some improvements that can be made.
For example, when you have the $queried_post object, you don't need to create additional variables for content and title. You can use this object properties to get the values.
Also you can use wordpress get_the_post_thumbnail to show featured image.
Some formatting and it's almost perfect.
$post_id = 26; // post id
$queried_post = get_post($post_id);
echo '<a href="' . get_permalink( $post_id ) . '" title="' . $queried_post->post_title . '">';
echo $queried_post->post_title;
echo '</a>';
if ( has_post_thumbnail( $post_id ) ) {
echo get_the_post_thumbnail( $post_id, 'full', array('width' => '100%') );
}
echo $queried_post->post_content;
try this code
$post_id = 26;
if ( has_post_thumbnail($post_id) )
{
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_id),’thumbnail’ );
}
echo '<a href="' . get_the_permalink($post_id) . '" title="' .get_the_title($post_id). '">';
echo get_the_title($post_id);
echo '</a>';
echo '<img width="100%" src="' . $image_src[0] . '">';
echo get_the_content($post_id);
In response to your comment, if you want to simplify this line:
echo '<a href="' . $perma . '" title="' . $title . '">';
echo $title;
echo '</a>';
You could do:
echo '' . $title . '';
So replacing multiple echo; with the "." to 'continue' without breaks.
Related
I'm trying to separate the Featured Image from the Products Variation Gallery.
I was able to split the Featured Image, thanks to a custom shortcode placed in function.php, and I even managed to do an on-click action - Lightbox as well as a custom size option.
However, for the past few days I've been having trouble setting up the same feature but for the variation products gallery.
The code that I used to output the featured photo is this:
global $product;
$title = $product->get_name();
$image_id = $product->get_image_id();
$image_url = wp_get_attachment_url( $image_id );
$image_src = wp_get_attachment_image_src( $image_id, array( 740, 360 ) );
echo '<center><a href="' . $image_url . '" data-lightbox="variation-product" data-title="' . $product->get_name() . '">';
echo '<img src="' . $image_src[0] . '" width="' . $image_src[1] . '" height="' . $image_src[2] . '" alt="' . $product->get_name() . '">';
echo '</a></center>';
And this one is for the variation products gallery:
global $product;
$attachment_ids = $product->get_gallery_image_ids();
foreach ( $attachment_ids as $attachment_id ) {
$image_url = wp_get_attachment_url( $attachment_id );
$image_src = wp_get_attachment_image_src( $attachment_id, array( 740, 360 ) );
echo '<center><a href="' . $image_url . '" data-lightbox="variation-product" data-title="' . $product->get_name() . '">';
echo '<img src="' . $image_src[0] . '" width="' . $image_src[1] . '" height="' . $image_src[2] . '" alt="' . $product->get_name() . '">';
echo '</a></center>';
}
And this is how it looks. This is the main problem. I tried to limit the picutres like this:
$i = 0;
foreach ( $attachment_ids as $attachment_id ) {
if ($i == 1) break;
/* the rest of the code */
$i++;
}
and this is the final result. You wonder where the problem is? Well... There is.
This variant of the code completely removes all variation images. The result I am trying to achieve is:
When we have more than one variable image, only the first one is shown on the product page. When the first variation photo is opened in the Lightbox, all other photos should be visible.
How can I do that? The theme that I'm using is Woodmart, by XTemos.
I'm trying to insert some code in WooCommerce wc_dropdown_variation_attribute_options function.
I got line like this:
$html .= '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $args['selected'] ), $term->slug, false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name, $term, $attribute, $product ) ) . '</option>';
and I need to insert this code:
'<img src="' . esc_attr($variation['image']['thumb_src']) . '">' .
if I just insert it like that - nothing shows, but when I display it inside esc_html, the code is shown as plain text and I see it configured correctly.
How do I need to insert code here to display image?
I just tested this and it's showing the image.
function woocommerce_dropdown_variation_attribute_options_html( $html, $args ) {
$variation['image']['thumb_src'] = "https://i.picsum.photos/id/230/50/50.jpg";
$html = '<img src="' . esc_attr($variation['image']['thumb_src']) . '">';
$html.= '<img src="' . esc_attr($variation['image']['thumb_src']) . '">';
return $html;
}
add_filter( 'woocommerce_dropdown_variation_attribute_options_html', 'woocommerce_dropdown_variation_attribute_options_html', 10, 2 );
Hey guys I have been asked to brand the site for my company. We are using Wordpress for it and basically each User Role is a different group that needs to see a different logo on the site.
I am using Eduma by Thimpress as my theme. I feel like it should be as simple as checking what user_role is logged in and changed the logo image, but I don't know where to start to put that condition.
Any help or guidance is much appreciated!
Below is the code I think creates the logo:
<?php
add_action( 'thim_logo', 'thim_logo', 1 );
// logo
if ( !function_exists( 'thim_logo' ) ) :
function thim_logo() {
$thim_logo = get_theme_mod( 'thim_logo', false );
$style = '';
if ( !empty( $thim_logo ) ) {
if ( is_numeric( $thim_logo ) ) {
$logo_attachment = wp_get_attachment_image_src( $thim_logo, 'full' );
if ( $logo_attachment ) {
$src = $logo_attachment[0];
$style = 'width="' . $logo_attachment[1] . '" height="' . $logo_attachment[2] . '"';
} else {
// Default image
// Case: image ID from demo data
$src = get_template_directory_uri() . '/images/logo.png';
$style = 'width="153" height="40"';
}
} else {
$src = $thim_logo;
}
} else {
// Default image
// Case: The first install
$src = get_template_directory_uri() . '/images/logo-sticky.png';
$style = 'width="153" height="40"';
}
$src = thim_ssl_secure_url($src);
echo '<a href="' . esc_url( home_url( '/' ) ) . '" title="' . esc_attr( get_bloginfo( 'name' ) ) . ' - ' . esc_attr( get_bloginfo( 'description' ) ) . '" rel="home" class="no-sticky-logo">';
echo '<img src="' . $src . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" ' . $style . '>';
echo '</a>';
}
endif;
add_action( 'thim_sticky_logo', 'thim_sticky_logo', 1 );
// get sticky logo
if ( !function_exists( 'thim_sticky_logo' ) ) :
function thim_sticky_logo() {
$sticky_logo = get_theme_mod( 'thim_sticky_logo', false );
$style = '';
if ( !empty( $sticky_logo ) ) {
if ( is_numeric( $sticky_logo ) ) {
$logo_attachment = wp_get_attachment_image_src( $sticky_logo, 'full' );
if ( $logo_attachment ) {
$src = $logo_attachment[0];
$style = 'width="' . $logo_attachment[1] . '" height="' . $logo_attachment[2] . '"';
} else {
// Default image
// Case: image ID from demo data
$src = get_template_directory_uri() . '/images/logo-sticky.png';
$style = 'width="153" height="40"';
}
} else {
$src = $sticky_logo;
}
} else {
// Default image
// Case: The first install
$src = get_template_directory_uri() . '/images/logo-sticky.png';
$style = 'width="153" height="40"';
}
$src = thim_ssl_secure_url($src);
echo '<a href="' . esc_url( home_url( '/' ) ) . '" rel="home" class="sticky-logo">';
echo '<img src="' . $src . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" ' . $style . '>';
echo '</a>';
}
endif;
In the file where you set the logo put this code there.
$user = wp_get_current_user();
if ( in_array( 'author', (array) $user->roles ) ) {
//logo for author role
}
That depends on the layout of the theme. Likely, it's in the header.php.
The way to proceed is something like this:
Create a child theme
https://codex.wordpress.org/Child_Themes
Find out what file outputs the link to the logo
Probably header.php but that depends on the theme; ask the theme developer if unsure.
Use for example wp_get_current_user()
https://codex.wordpress.org/Function_Reference/wp_get_current_user
Do a php switch on the outcome of that and output various links
http://php.net/manual/en/control-structures.switch.php
I am new to PHP, currently getting error saying
Fatal error: Call to a member function url() on a non-object on line 8
Below is the code I am trying
<?php
$subpages = $site->pages()->children()->visible();
$image_url = $subpages->image()->url();
$title = $subpage->title()->text();
foreach($subpages as $subpage) {
echo '<div class="col-md-4">';
echo '<h2>' . $title . '</h2>';
echo '<a href="' . $subpage->url() . '" title="' . $title . '">';
echo '<img src="' . $image_url . '" alt="' . $title . '" class="img-responsive img-thumbnail">';
echo '</a>';
echo '</div>';
}
?>
Here's your code corrected:
<?php
$subpages = $site->children()->visible();
foreach($subpages as $subpage) {
$image_url = $subpage->image()->url();
$title = $subpage->title()->html();
echo '<div class="col-md-4">';
echo '<h2>' . $title . '</h2>';
echo '<a href="' . $subpage->url() . '" title="' . $title . '">';
echo '<img src="' . $image_url . '" alt="' . $title . '" class="img-responsive img-thumbnail">';
echo '</a>';
echo '</div>';
}
?>
What's wrong with your code:
You should use $site->children() to list all children of the site
See kirby docs
You're defining $image_url and $title before the foreach which is not correct. I moved them just at the beginning of the foreach
loop. Also corrected image_url to use subpage instead of subpages.
You're using text() on your title. That doesn't exist, use either kirbytext() or html() depending on what you want to do. See the
docs.
You should check what are the values and methods in $subpages? i think there is problem when you assigning the values to the variable.
i've created latest post container, which i'm trying to create in a shortcode, however when i add the excerpt into the div it is not being shown as it would normally. it is being pushed before the div. The test string after the excerpt is being placed the correct place inside in the div. How come the excerpt is being pushed to the top?
if($insta->have_posts()) :
while($insta->have_posts()) :
$insta->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
$post_excerpt = the_excerpt();
$permalink = get_permalink();
$output = '<div class="betting-item">'
. '<div class="betting-thumb">'
. '<a href=" '. $permalink .' ">'
. '<img class="thumbnail" src="'. $thumb_url[0] .'" alt="0" />" </a>'
. '</div>'
. '<div class="betting-description">'
. '<h6>Instalocket</h6>'
. '<div class="meta">November 18, 2014 • No Comments</div>'
. '<div>' . $post_excerpt[0] . ' test</div>'
. '</div>'
. '</div>';
endwhile;
endif;
That is happening because the_excerpt() will print the except there and then (so before the div), you need to use get_the_excerpt () which will allow you to print it out at a later stage (in the div).
EDIT:
This should work and only print the excerpt on the first loop:
if($insta->have_posts()) :
$count = 0;
while($insta->have_posts()) :
$insta->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
if($count == 0)
{
$post_excerpt = the_excerpt();
$count = 1;
}
else
{
$post_excerpt = '';
}
$permalink = get_permalink();