Ghost Blog posts sort order - ghost-blog

Has anyone figured out a simple/quick way to use a custom post sort order with Ghost?
Specifically I would like to implement a theme that uses Ghost's tag-{slug}.hbs functionality to render tag specific pages that order posts oldest>newest instead of the default newest>oldest.
Any ideas vs. hacking at ghost core and making a big mess?
Thanks

See : https://github.com/TryGhost/Ghost/issues/5602,
To order post oldest > newest, edit the core\server\models\post.js file, find the orderDefaultOptions function and change the published_at value ('ASC') :
orderDefaultOptions: function orderDefaultOptions() {
return {
status: 'ASC',
published_at: 'ASC',
updated_at: 'DESC',
id: 'DESC'
};
},

You can not make the blog engine return a different sort order. But what you can do is use template code to throw away the results provided by the engine and make it fetch new results with the #get helper.
{{#has any="tag.feature_image"}}
{{#get "posts" filter="tags.slug:{{tag.slug}}" limit=100 order="published_at asc"}}
<!-- `posts` from the parent context is overwritten by get -->
{> "loop"}}
{{/get}}
{{else}}
<!-- default loop -->
{{> "loop"}}
{{/has}}
I for example wanted to list posts in tag.hbs in chronological order if the Tag had a featured_image. So for such tags I use the #get helper to get the posts in chronological order (order="published_at asc"). If not the posts already provided by the blogging engine in reverse order are displayed.

add this line
"postinstall": "sed -i \"s/published_at: 'DESC'/published_at: 'ASC'/g\" node_modules/ghost/core/server/models/post.js && sed -i \"s/_at DESC,'/_at ASC,'/g\" node_modules/ghost/core/server/models/post.js",
inside scripts section in your package.json it should work

Related

Can I display a custom field inside a Loop Query block?

I’m fairly new to Wordpress and I’m trying all my best to learn, but I can’t find a solution for this one…
I’ve been using the Twenty Twenty-Two theme and I’ve added a custom field ("city") to my articles.
Of course when I add a Query Loop block on my homepage, the visual editor allows me to use Titles, Categories and so on… but it won’t let me use my custom field.
I tried adding what follows to functions.php:
add_shortcode( 'city', 'return_my_custom_value' );
function return_my_custom_value() {
$myCity = do_shortcode(get_post_meta(get_the_ID(), 'city', true));
return $myCity;
}
But it won’t work, because then I use the following shortcode inside my Query Loop block:
[city]
and it shows a list of my articles all with the same 'city' value, repeated — which is the one I assigned to the page I’m showing the query on (my homepage).
Any solution for this? I would really appreciate each suggestion. Thank you so much in advance!
We have checked the code you are not calling shortcode inside the query loop. we have updated the code you can use the code. Please check below the updated code and add in the functions.php file.
add_shortcode( 'city', 'return_my_custom_value' );
function return_my_custom_value() {
$myCity = get_post_meta(get_the_ID(), 'city', true);
return $myCity;
}
Please use the following shortcode inside your query loop block
echo do_shortcode('[city]');

How to add code between <head> and </head> in Drupal but exclude some nodes

I'm trying to add Google AdSense 'page level ads' to my Drupal website. It requires pasting some code between the two tags <head> and </head>. I want to however exclude some nodes from this. Since they are all the same node type, they will load from the same page.tpl.php file. How do I do this? Thanks.
Like everything in Drupal, there are many ways to do this. You could check for specific node ids in a preprocess function and then only add that code when the page is rendering specific node ids.
function themename_preprocess_page(&$vars) {
//check and see if we're rendering a node and if the current nid is in the a
if (isset($variables['node'] && in_array($variables['node']->nid, array('1','2','3','4','5')) {
drupal_add_js('your adsense code here',
array('type' => 'inline', 'scope' => 'header');
}
}
To make it more manageable for people, what I would do is add a checkbox field for that node type for "No ads" or something. This will give you a process for removing ads from that node from the admin interface and you won't need to constantly tinker with code to exclude certain node ids.
Now make (or add to) your themename_preprocess_page function.
//psuedo code -- don't copy and paste
theme_preprocess_page(&$vars) {
if ((isset($variables['node']->type) && $variables['node']->type == 'your_node_type') && isset($variables['node']->field_checkbox[LANGUAGE_NONE][0]) && $variables['node']->field_checkbox[LANGUAGE_NONE][0][value]) {
drupal_add_js(your javascript ad code);
}
}

Working with forms in drupal 8

I am facing problem with "#markup" in form API.
In Drupal 7 we can use "#markup" form element which look like this:
<?php
$form['test'] = array(
'#type' => 'markup',
'#markup' => '<div><script src="http://localhost/drupal7/sites/all/libraries/test.js"></script></div>',
);
?>
//Here is my custom test.js
(function($) {
Drupal.behaviors.test = {
attach: function(context, settings) {
console.log('testttt');
document.write('*Hello, there!*');
}
};
})(jQuery);
and above code will print "Hello, there!" when form will be render.
Now in Drupal 8 I am using below code but it prints nothing.
<?php
$form['test'] = array(
'#markup' => '<div><script src="http://localhost/project8/sites/all/libraries/test.js"></script></div>',
);
?>
So how can implement this functionality in Drupal 8, which is already working in Drupal 7 .
Under script tag it can be local script or external script..
Please help...
Thanks
In Drupal 8, using "#markup" is not the proposed method to attach javascript files.
You can define libraries in your custom module or theme and attach the library to your form. The library can contain multiple js and (or) css files.
To define a library in your module:
Suppose your module name is "my_module", create a file "my_module.libraries.yml" in your module folder and specify the js and css files like this
form-script:
version: 1.x
css:
theme:
css/form.css: {}
js:
js/form.js: {}
js/form-ajax.js: {}
dependencies:
- core/jquery
In order to attach this library to your form:
$form['#attached']['library'][] = 'my_module/form-script';
Then clear cache. The js and css files will be loaded in the same order as you mentioned in the libraries.yml file.
You can define multiple libraries in the same "my_module.libraries.yml" file.
#markup still works in Drupal 8, but now it is filtered before output. As it is stated in Render API overview:
#markup: Specifies that the array provides HTML markup directly. Unless the markup is very simple, such as an explanation in a paragraph tag, it is normally preferable to use #theme or #type instead, so that the theme can customize the markup. Note that the value is passed through \Drupal\Component\Utility\Xss::filterAdmin(), which strips known XSS vectors while allowing a permissive list of HTML tags that are not XSS vectors. (I.e, and are not allowed.) See \Drupal\Component\Utility\Xss::$adminTags for the list of tags that will be allowed. If your markup needs any of the tags that are not in this whitelist, then you can implement a theme hook and template file and/or an asset library. Aternatively, you can use the render array key #allowed_tags to alter which tags are filtered.
As an alternative you may use FormattableMarkup:
'#markup' => new FormattableMarkup('<div><script src="http://localhost/project8/sites/all/libraries/test.js"></script></div>', []),
though it is not recommended in this case.

How to change the number of comments per page for a single node in Drupal 7?

i would like to change the comments per page for only a single node. Let's say that the defaul number of comments per page for the content type "article" is 50 and i want change this to 10 only for the article with nid=171.
$node = menu_get_object('node',1); if($node && $node->nid = 171) {......}
Any hint? Thanks.
If you want to control the number of comments on individual pages, I would suggest installing Views module which allow for easy creation of any lists of things, including comments. With Views, you can create a block with comments for any node and use that instead of regular comments block.
Also, you can have several different blocks with different number of comments, and attach whatever block you want to whatever node. Using Context module would help too, it allows to assign blocks and other elements to pages with much more sophisticated controls than Drupal core block management.
Check out can this be helpful to you:
https://drupal.stackexchange.com/questions/7450/comment-per-page-settings
Since it's a form you have to alter it and change comments number from code.
i found a solution even if a bit rude. To disable the node's default comments i used a preprocess function in template.php file of my subtheme and disbled the $content['comments'] variable used in the comment-wrapper.tpl.php template:
function MYSUBTHEMENAME_preprocess_comment_wrapper(&$vars){
$node = menu_get_object();
if($node):
if($node->type === 'forum' && $node->nid == 171):
unset($vars['content']['comments']);
endif;
endif;
}
Note that i did it for comment wrapper variables/template and not for the node vfariables/template in order to unset the comments but to keep the comment form.
Then i created a view block of comments with a filter for the nid of the comment's node.
This works well for my purposes but if you find a more elegant way let me know. :)
I spent a time looking comment.module and found this in comment_form_node_type_form_alter
$form['comment']['comment_default_per_page'] = array(
'#type' => 'select',
'#title' => t('Comments per page'),
'#default_value' => variable_get('comment_default_per_page_' . $form['#node_type']->type, 50),
'#options' => _comment_per_page(),
);
So I put that in my template.php
function THEMENAME_preprocess_page(&$variables){
if (isset($variables['node']->type)) {
if ($variables['node']->type == 'forum') {
variable_set('comment_default_per_page_' . 'forum', 4);
}
}
}
It worked for me, I hope it helps.

Adding custom code to <head> in Drupal

I'm trying to figure out where the <head> for all of the pages in Drupal is (I'm using Orange theme if it matters). I have to add analytics code into the <head>.
Inside which file would I find the <head>?
Use drupal_set_html_head() by placing this in your themes template.php file. If the function MYTHEMENAME_preprocess_page() already exists insert what is inside the {closure} brackets below (before the $vars['head'] if that exists in it as well) :
function MYTHEMENAME_preprocess_page(&$vars, $hook) {
// say you wanted to add Google Webmaster Tools verification to homepage.
if (drupal_is_front_page()) {
drupal_set_html_head('<meta name="google-site-verification" content="[string from https://www.google.com/webmasters/verification/verification]" />');
$vars['head'] = drupal_get_html_head();
}
}
In template.php of your theme folder :
function your_theme_preprocess_html(&$variables) {
$appleIcon57px = array(
'#tag' => 'link',
'#attributes' => array(
'rel' => 'apple-touch-icon',
'href' => '/images/ICONE-57.png',
'type' => 'image/png',
'media' => 'screen and (resolution: 163dpi)'
)
);
drupal_add_html_head($appleIcon57px, 'apple-touch-icon57');
}
If you look in your theme folder you'll see page.tpl.php, that is the template for the site. You can add the code there most likely.
How to change a page meta description and keywords in Drupal 6
One another solution is to use blocks in header these can also managed very effectively using css.
All you have to do is to go to Structure->Blocks then create new block.
Then select theme corresponding to it and position in which section you want to show that block.
Custom html like can be added. And can be handled from that id.
It allow me to handle page structure very easily.
In the theme folder (e.g. themes/orange) there is a templates folder with the file html.tpl.php
In this file you can freely add what you need to the head section and it will be added to every page.
there is a google analyitics module that will accomplish this for you with just your key.

Resources